﻿Imports System.Runtime.InteropServices
Imports Microsoft.Win32.SafeHandles
Imports System.IO
Imports System.Threading
Imports System.Text

Friend Module NativeMethods

#Region "Driver"
    <DllImport("kernel32.dll", SetLastError:=True, CharSet:=CharSet.Unicode)>
    Public Function CreateFile(lpFileName As String, <MarshalAs(UnmanagedType.U4)> dwDesiredAccess As FileAccess, <MarshalAs(UnmanagedType.U4)> dwShareMode As FileShare, lpSecurityAttributes As IntPtr, <MarshalAs(UnmanagedType.U4)> dwCreationDisposition As FileMode, <MarshalAs(UnmanagedType.U4)> dwFlagsAndAttributes As FileAttributes, hTemplateFile As IntPtr) As IntPtr
    End Function

    <DllImport("kernel32.dll", SetLastError:=True)>
    Public Function CloseHandle(ByVal hObject As IntPtr) As <MarshalAs(UnmanagedType.Bool)> Boolean
    End Function

    <DllImport("advapi32.dll", CharSet:=CharSet.Auto, SetLastError:=True)>
    Public Function OpenSCManager(ByVal machineName As String, ByVal databaseName As String, ByVal desiredAccess As Int32) As IntPtr
    End Function

    <DllImport("advapi32.dll", SetLastError:=True, CharSet:=CharSet.Auto)>
    Public Function CreateService(ByVal hSCManager As IntPtr, ByVal serviceName As String, ByVal displayName As String, ByVal desiredAccess As Int32, ByVal serviceType As Int32, ByVal startType As Int32, ByVal errorcontrol As Int32, ByVal binaryPathName As String, ByVal loadOrderGroup As String, ByVal TagBY As Int32, ByVal dependencides As String, ByVal serviceStartName As String, ByVal password As String) As IntPtr
    End Function

    <DllImport("advapi32.dll", SetLastError:=True, CharSet:=CharSet.Auto)>
    Public Function OpenService(ByVal hSCManager As IntPtr, ByVal lpServiceName As String, ByVal dwDesiredAccess As Int32) As IntPtr
    End Function

    Declare Function StartService Lib "advapi32.dll" Alias "StartServiceA" (ByVal hService As IntPtr, ByVal dwNumServiceArgs As Long, ByVal lpServiceArgVectors As String) As Boolean

    <DllImport("advapi32.dll", SetLastError:=True)>
    Public Function CloseServiceHandle(ByVal serviceHandle As IntPtr) As Boolean
    End Function

    <DllImport("advapi32.dll", SetLastError:=True)>
    Public Function DeleteService(ByVal hService As IntPtr) As Boolean
    End Function
#End Region

    Public Declare Auto Function GetDiskFreeSpace Lib "kernel32.dll" (ByVal lpRootPathName As String, ByRef lpSectorsPerCluster As UInt32, ByRef lpBytesPerSector As UInt32, ByRef lpNumberOfFreeClusters As UInt32, ByRef lpTotalNumberOfClusters As UInt32) As Integer
    Public Declare Auto Function GetVolumeInformation Lib "kernel32.dll" (ByVal RootPathName As String, ByVal VolumeNameBuffer As StringBuilder, ByVal VolumeNameSize As UInt32, ByRef VolumeSerialNumber As UInt32, ByRef MaximumComponentLength As UInt32, ByRef FileSystemFlags As UInt32, ByVal FileSystemNameBuffer As StringBuilder, ByVal FileSystemNameSize As UInt32) As UInt32

    <DllImport("shell32.dll")> _
    Public Sub SHChangeNotify(ByVal wEventID As Integer, ByVal uFlags As Integer, ByVal dwItem1 As String, ByVal dwItem2 As String)
    End Sub

    <DllImport("user32.dll", SetLastError:=True)> _
    Public Sub SendMessageTimeout(ByVal windowHandle As IntPtr, ByVal Msg As Integer, ByVal wParam As IntPtr, ByVal lParam As IntPtr, ByVal flags As Integer, ByVal timeout As Integer, ByRef result As IntPtr)
    End Sub

    Public Declare Auto Function QueryDosDevice Lib "kernel32.dll" (ByVal lpDeviceName As String, ByRef lpTargetPath As String, ByVal ucchMax As UInteger) As UInteger

    <DllImport("kernel32.dll", CharSet:=CharSet.Auto, SetLastError:=True)>
    Public Function GetFileTime(ByVal hFile As IntPtr, <Out()> ByRef lpCreationTime As Long, <Out()> ByRef lpLastAccessTime As Long, <Out()> ByRef lpLastWriteTime As Long) As Boolean
    End Function

    <DllImport("kernel32.dll", CharSet:=CharSet.Auto, SetLastError:=True)>
    Public Function SetFileTime(ByVal hFile As IntPtr, ByRef lpCreationTime As Long, ByRef lpLastAccessTime As Long, ByRef lpLastWriteTime As Long) As Boolean
    End Function

#Region "DeviceIoControl"
    <DllImport("kernel32.dll", ExactSpelling:=True, SetLastError:=True, CharSet:=CharSet.Unicode)>
    Public Function DeviceIoControl(ByVal hDevice As IntPtr, ByVal dwIoControlCode As TC_IOCTL, ByVal lpInBuffer As IntPtr, ByVal nInBufferSize As UInteger, ByRef lpOutBuffer As IntPtr, ByVal nOutBufferSize As UInteger, ByRef lpBytesReturned As UInteger, ByVal lpOverlapped As IntPtr) As Boolean
    End Function

    <DllImport("kernel32.dll", EntryPoint:="DeviceIoControl", SetLastError:=True, CharSet:=CharSet.Unicode)>
    Public Function DeviceIoControlMount(ByVal hDevice As IntPtr, ByVal dwIoControlCode As TC_IOCTL, ByVal lpInBuffer As MOUNT_STRUCT, ByVal nInBufferSize As UInteger, ByRef lpOutBuffer As MOUNT_STRUCT, ByVal nOutBufferSize As UInteger, ByRef lpBytesReturned As UInteger, ByVal lpOverlapped As IntPtr) As Boolean
    End Function

    <DllImport("kernel32.dll", EntryPoint:="DeviceIoControl", SetLastError:=True, CharSet:=CharSet.Unicode)>
    Public Function DeviceIoControlUnmount(ByVal hDevice As IntPtr, ByVal dwIoControlCode As TC_IOCTL, ByVal lpInBuffer As UNMOUNT_STRUCT, ByVal nInBufferSize As UInteger, ByRef lpOutBuffer As UNMOUNT_STRUCT, ByVal nOutBufferSize As UInteger, ByRef lpBytesReturned As UInteger, ByVal lpOverlapped As IntPtr) As Boolean
    End Function

    <DllImport("kernel32.dll", EntryPoint:="DeviceIoControl", SetLastError:=True, CharSet:=CharSet.Unicode)>
    Public Function DeviceIoControlListMounted(ByVal hDevice As IntPtr, ByVal dwIoControlCode As TC_IOCTL, ByVal lpInBuffer As MOUNT_LIST_STRUCT, ByVal nInBufferSize As UInteger, ByRef lpOutBuffer As MOUNT_LIST_STRUCT, ByVal nOutBufferSize As UInteger, ByRef lpBytesReturned As UInteger, ByVal lpOverlapped As IntPtr) As Boolean
    End Function

    <DllImport("kernel32.dll", EntryPoint:="DeviceIoControl", SetLastError:=True, CharSet:=CharSet.Unicode)>
    Public Function DeviceIoControlOpenTest(ByVal hDevice As IntPtr, ByVal dwIoControlCode As TC_IOCTL, ByVal lpInBuffer As OPEN_TEST_STRUCT, ByVal nInBufferSize As UInteger, ByRef lpOutBuffer As OPEN_TEST_STRUCT, ByVal nOutBufferSize As UInteger, ByRef lpBytesReturned As UInteger, ByVal lpOverlapped As IntPtr) As Boolean
    End Function

    <DllImport("kernel32.dll", EntryPoint:="DeviceIoControl", SetLastError:=True, CharSet:=CharSet.Unicode)>
    Public Function DeviceIoControlVolProp(ByVal hDevice As IntPtr, ByVal dwIoControlCode As TC_IOCTL, ByVal lpInBuffer As VOLUME_PROPERTIES_STRUCT, ByVal nInBufferSize As UInteger, ByRef lpOutBuffer As VOLUME_PROPERTIES_STRUCT, ByVal nOutBufferSize As UInteger, ByRef lpBytesReturned As UInteger, ByVal lpOverlapped As IntPtr) As Boolean
    End Function
#End Region

    Public Enum MEDIA_TYPE As UInteger
        Unknown
        F5_1Pt2_512
        F3_1Pt44_512
        F3_2Pt88_512
        F3_20Pt8_512
        F3_720_512
        F5_360_512
        F5_320_512
        F5_320_1024
        F5_180_512
        F5_160_512
        RemovableMedia
        FixedMedia
        F3_120M_512
        F3_640_512
        F5_640_512
        F5_720_512
        F3_1Pt2_512
        F3_1Pt23_1024
        F5_1Pt23_1024
        F3_128Mb_512
        F3_230Mb_512
        F8_256_128
        F3_200Mb_512
        F3_240M_512
        F3_32M_512
    End Enum

    <StructLayout(LayoutKind.Sequential)> _
    Public Structure DISK_GEOMETRY
        Public Cylinders As Long
        Public MediaType As MEDIA_TYPE
        Public TracksPerCylinder As Integer
        Public SectorsPerTrack As Integer
        Public BytesPerSector As Integer

        Public ReadOnly Property disksize() As Long
            Get
                Return Cylinders * CLng(TracksPerCylinder) * CLng(SectorsPerTrack) * CLng(BytesPerSector)
            End Get
        End Property
    End Structure

    <StructLayout(LayoutKind.Sequential)>
    Public Structure PARTITION_INFORMATION
        Public StartingOffset As Long
        Public PartitionLength As Long
        Public HiddenSectors As Integer
        Public PartitionNumber As Integer
        Public PartitionType As Byte

        <MarshalAs(UnmanagedType.I1)>
        Public BootIndicator As Boolean

        <MarshalAs(UnmanagedType.I1)>
        Public RecognizedPartition As Boolean

        <MarshalAs(UnmanagedType.I1)>
        Public RewritePartition As Boolean
    End Structure

    <StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Unicode)>
    Public Structure DEV_BROADCAST_VOLUME
        Dim dbcv_size As UInteger
        Dim dbcv_devicetype As UInteger
        Dim dbcv_reserved As UInteger
        Dim dbcv_unitmask As UInteger
        Dim dbcv_flags As UShort
    End Structure

End Module